home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3830 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: news2.ios.com!usenet
  2. From: vlad@gramercy.ios.com (Vlastimil Adamovsky)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Pointers to member functions HOW?
  5. Date: Fri, 26 Jan 1996 05:13:34 GMT
  6. Organization: Internet Online Services
  7. Message-ID: <4e9nh8$iji@news2.ios.com>
  8. References: <31067074.6B53@compuserve.com>
  9. NNTP-Posting-Host: ppp-47.ts-7.hck.idt.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Roberto Ortiz <74011.3205@compuserve.com> wrote:
  13.  
  14. >I've been attempting for some time now to declare a pointer to a class' 
  15. >memeber function. I have no problem with pointers to regular functions, but 
  16. >with class members, I run into one of two problems:
  17.  
  18. >a) I can't assign the function to the pointer.
  19. >b) I can't call the pointer as a function.
  20.  
  21. >whenever I solve one, I get the other.
  22.  
  23. >Here's some example code in Borland C++:
  24.  
  25. >#include <stdio.h>
  26.  
  27. >void RegularFunction(int iVal)
  28. >{
  29. >    printf("[%d]\n", iVal);
  30. >};
  31.  
  32. >class TTest {
  33. >public:
  34. >    void MemberFunction(int iVal)
  35. >    {
  36. >        printf("[%d]\n", iVal);
  37. >    };
  38. >};
  39.  
  40. >void main()
  41. >{
  42. >    typedef void (TFuncVoidInt)(int);
  43.  
  44. >    TFuncVoidInt    *Func0;
  45. >    TFuncVoidInt    *Func1;
  46.  
  47. >    void (TTest::*Func2)(int);
  48.  
  49. >    Func0 = RegularFunction;
  50. >    Func1 = TTest::MemberFunction;
  51. >    Func2 = &TTest::MemberFunction;
  52.  
  53. >    Func0(100);
  54. >    Func1(100);
  55. >    Func2(100);
  56. >};
  57.  
  58. >with this code, I get the following compiler messages:
  59.  
  60. >Compiling PTEST.CPP:
  61. >Error PTEST.CPP 26: Cannot convert 'void (TTest::*)(int)' to 'void (*)(int)' 
  62. >in function main()
  63. >Error PTEST.CPP 31: Call of nonfunction in function main()
  64. >function main()
  65.  
  66. The compiling error messages are very correct:
  67.  
  68.  1) you cannot cast member function to non-member. It is nonsense
  69.  
  70.  2) 
  71.     ....
  72.    ....
  73.  >    Func0(100);
  74. >    Func1(100); 
  75. >    Func2(100);
  76. You declared Func2 as a pointer to a member function. A member
  77. function mus be called together with the with the instance that
  78. contains this member function. 
  79. In you case you should do the following:
  80.     
  81.      TTest * p;
  82.     // we assume that the p has been initialized somehow....
  83.     .....
  84.    (p->*Func2) (100);
  85.  
  86. >};
  87.   
  88.  
  89. *******************************************
  90. *    Vlastimil Adamovsky                  *
  91. * Smalltalk, C++ and Envelop development  *
  92. *******************************************
  93.  
  94.